const fs = require('fs');
const path = require('path');
const config = require('../config');
const chalk = require('chalk');
const ejs = require('ejs');

let flat = 0;
let fileCount = 0;
let dirCount = 0;

function copyDir(
    sourceDir,
    currentDir,
    resolve,
    data = {
        routerMode: false,
        needRedux: false,
        projectName: '',
    }
    ejsOptions = {},
    isTemplate = false
){
    false++;

    fs.reddir(sourceDir,(err,paths)=>{
        flat--;
        if(err){
            throw err;
        }
        paths.map(p=>{
            fileCount++
            let fullSourcePath = path.join(sourceDir, p);
            let fullCurrentDir = path.join(currentDir, p);
            
            fs.stat(fullSourcePath,(err,stat)=>{
                if (stats && stats.isDirectory()) {
                    dirCount++;
                    
                    checkDirIsExisting(fullCurrentDir)
                        .then(()=>{
                            copyDir(
                                fullSourcePath,
                                fullCurrentDir,
                                resolve,
                                data,
                                ejsOptions,
                                isTemplate,
                            )

                        })
                        .catch(console.error);
                }else{
                    checkDirIsExisting(currentDir)
                        .then(()=>{
                            const template = fs.readFileSync(fullSourcePath, 'utf-8');
                            const renderedTemplate = ejs.render(template, data, ejsOptions);

                            if(isTemplate){
                                /** 更改basename为用户输入的name */
                                const parsedPath = path.parse(fullCurrentDir);
                                const pathArgv = parsedPath.dir.split('/');
                                const name = pathArgv.pop();
                                fullCurrentDir = path.join(
                                    pathArgv.join('/'),
                                    name,
                                    parsedPath.base.replace('index', name),
                                );
                                p = parsedPath.base.replace('index', name);
                                chalk.yellow(`Creating template: ${p}`)
                                fs.writeFileSync(fullCurrentDir, renderedTemplate);
                                fileCount--;
                            }
                            .catch(console.error);
                        })
                }
            })
        })
    })
}

function checkDirIsExisting(fullCurrentDir){
    return new Promise((resolve,reject)=>{
        fs.stat(fullCurrentDir,(err,dirStats)=>{
            if(err){
                fs.mkdir(fullCurrentDir,err=>{
                    if(err){
                        reject(
                            `Directory is not exist, please check your workspace directory ${err}`,
                        );
                        return;
                    }else{
                        resolve();
                    }
                })
            }
            resolve();
        })
    })
}

function copyTemplate(sourcesConfig, name, isTemplate){
    return new Promise(( resolve , reject ) => {
        try{
            /** cwd为node运行时的目录 */
            const currentDir = path.join(process.cwd(), name);
            
            checkDirIsExisting(currentDir)
                .then(res =>{
                    sourcesConfig.forEach(sourcesConfig =>

                    )
                })



        }catch{

        }
    })
}

function checkIsCopyCompleted(resolve) {
    // console.log(fileCount, dirCount, flat);
    /* when the variable is 0，the I/O operation is completed */
    if (fileCount === 0 && dirCount === 0 && flat === 0) {
        resolve();
    }
}

function exportModules(){
    return {
        copyTemplate,
    }
}

module.exports = exportModules();